// pages/addinfo/addinfo.js
const request=require('../../util/request.js')
const instruct = require('../../util/tspl')
import bleDisNetwork from '../../util/bleDisNetwork'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    list:[],
    page: 1,//页码
    limit: 5,//每页数量
    type:0,//可不传参数
    keyword:"",//可不传参数
    add_time:"",//可不传参数
    p_no:"",//项目号
    count: 0,
    flag: true,
    projectbody:[],//项目主体信息
    datastatus:2,//查看 2全部/1以完善/0未完成 
    datafind:"",//搜索关键字
    date:"",
    mindate:Math.round((new Date() - 365*86400*1000)),
    findtime:"",//时间范围查找
    __returnParams:false,
    selectedItems: [] // 存储选中的完整数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      __returnParams:false,

    })
    this.data.p_no = options.p_no
    this.data.page = 1
    this.getList()

  },
  
  getList(search="",date="") {
    var that = this;
    let tmp = {
      // page:that.data.page,
      // limit:that.data.limit,
      keyword:that.data.datafind,
    }
    //页面功能定义 'order/get_sale_orders'|order/get_products_by_inter_id
    // request.requestGet('drawing/get_list', params, "加载中",function (res) {
    request.requestGet('fruit/get_barcode_list',tmp, "加载中",function (res) {
        console.log(res,"=====res")
      if(res.status === 200){
        that.setData({list:res.data})
      }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('走到onshow')
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    let name = `formData.${valueName}`;
    this.setData({
      [valueName]:event.detail
    });
  },
  onTabChange(event){
    if(event.detail.index === 1){
      this.data.datastatus = 1
    }else if(event.detail.index === 2){
      this.data.datastatus = 0
    }else if(event.detail.index === 0){
      this.data.datastatus = 2
    }
    this.data.page = 1
    this.getList()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.data.page = 1
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },

  onSearch(e){
    console.log(e,"-------e")
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.getList(e.detail)
  },
  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },
  comment(event){
    let tmp = event.currentTarget.dataset.index
    let pic_no = ""
    for(let i = 0;i<this.data.projectbody.length;i++){
      if(this.data.projectbody[i].id === tmp){
        pic_no = this.data.projectbody[i].pic_no
      }
    }
    wx.navigateTo({
      url: `/pages/pic_no_list_process/pic_no_list_process?p_no=${tmp}&pic_no=${pic_no}`,
    })
  },
  edit(event){
    let tmpvalue1 = this.data.projectbody[event.currentTarget.dataset.index].FCheckDate
    let tmpvalue2 = this.data.projectbody[event.currentTarget.dataset.index].FBillNo
    let tmpvalue3 = this.data.projectbody[event.currentTarget.dataset.index].FBillerID
    let tmpvalue4 = this.data.projectbody[event.currentTarget.dataset.index].CustomerName
    let tmpvalue5 = this.data.projectbody[event.currentTarget.dataset.index].department_name
    let tmpvalue6 = this.data.projectbody[event.currentTarget.dataset.index].salesman_name
    let tmpvalue7 = this.data.projectbody[event.currentTarget.dataset.index].biller_name
    let tmp ={
      // FCheckDate:this.data.projectbody[event.currentTarget.dataset.index].FCheckDate,
      // FBillNo:this.data.projectbody[event.currentTarget.dataset.index].FBillNo,
      // FBillerID:this.data.projectbody[event.currentTarget.dataset.index].FBillerID,
      // CustomerName:this.data.projectbody[event.currentTarget.dataset.index].CustomerName,
      // department_name:this.data.projectbody[event.currentTarget.dataset.index].department_name,
      // salesman_name:this.data.projectbody[event.currentTarget.dataset.index].salesman_name,
      // biller_name:this.data.projectbody[event.currentTarget.dataset.index].biller_name,
    }
    // this.data.projectbody[event.currentTarget.dataset.index]
    console.log(tmp,"====tmptmp")
    wx.navigateTo({
      url: `/pages/pic_no_list_info/pic_no_list_info?value1=${tmpvalue1}&value2=${tmpvalue2}&value3=${tmpvalue3}&value4=${tmpvalue4}&value5=${tmpvalue5}&value6=${tmpvalue6}&value7=${tmpvalue7}`,
    })
  },

  setrem(e){
    wx.navigateTo({
      url: `/pages/pic_no_rember/pic_no_rember?FInterID=${e.currentTarget.dataset.index}&fbillno=${e.currentTarget.dataset.fbillno}`,
    })
  },

  onItemClick: function(e) {
    const item = e.currentTarget.dataset.item;
    const index = e.currentTarget.dataset.index;
    
    // 切换选中状态
    const list = this.data.list;
    const isSelected = !list[index].selected;
    
    // 更新列表选中状态
    this.setData({
      [`list[${index}].selected`]: isSelected
    });
    
    // 更新选中数据集合
    this.updateSelectedItems(item, isSelected);
    
    console.log('当前选中项:', this.data.selectedItems);
  },

  updateSelectedItems: function(item, isSelected) {
    let selectedItems = this.data.selectedItems;
    
    if (isSelected) {
      // 添加选中项
      selectedItems.push(item);
    } else {
      // 移除取消选中的项
      selectedItems = selectedItems.filter(selectedItem => 
        selectedItem.barcode !== item.barcode
      );
    }
    
    this.setData({
      selectedItems: selectedItems
    });
  },

  submit(event){
    var that = this
  wx.navigateBack({
    delta: 1,
    success: function() {
      // 确保页面完全激活后再处理数据
      setTimeout(() => {
        const currentPages = getCurrentPages();
        const currentPage = currentPages[currentPages.length - 1];
        currentPage.onSubmitSuccess(that.data.selectedItems);
      }, 200);
    }
  });
  },

})